package com.jcli.fx3u;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import com.jcli.HslCommunication.Profinet.Melsec.MelsecA1ENet;
import com.jcli.HslCommunication.Core.Types.OperateResultExOne;

public class _02_PLCControlApp {
    private JFrame frame;
    private JTextArea textArea;

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    _02_PLCControlApp window = new _02_PLCControlApp();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public _02_PLCControlApp() {
        initialize();
    }

    private void initialize() {
        frame = new JFrame();
        frame.setBounds(100, 100, 400, 300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(new BorderLayout(0, 0));

        JPanel panel = new JPanel();
        frame.getContentPane().add(panel, BorderLayout.NORTH);

        JButton btnReadPLC = new JButton("读取PLC");
        panel.add(btnReadPLC);

        JButton btnWritePLC = new JButton("写入PLC");
        panel.add(btnWritePLC);

        textArea = new JTextArea();
        textArea.setEditable(false);
        JScrollPane scrollPane = new JScrollPane(textArea);
        frame.getContentPane().add(scrollPane, BorderLayout.CENTER);

        // 添加按钮点击事件处理
        btnReadPLC.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                readFromPLC();
            }
        });

        btnWritePLC.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                writeToPLC();
            }
        });
    }

    private void readFromPLC() {
        MelsecA1ENet melsec_net = new MelsecA1ENet("192.168.1.11", 3000);
        melsec_net.SetPersistentConnection();

        OperateResultExOne<Boolean> readD497 = melsec_net.ReadBool("D497");
        if (readD497.IsSuccess) {
            boolean d497Value = readD497.Content;
            if (d497Value) {
                OperateResultExOne<byte[]> readD500D700 = melsec_net.Read("D500", (short) 400); // 读取D500-D700，共计201个寄存器
                if (readD500D700.IsSuccess) {
                    textArea.append("读取成功：" + byteArrayToHexString(readD500D700.Content) + "\n");
                    melsec_net.Write("D497", false);
                } else {
                    textArea.append("读取D500-D700失败：" + readD500D700.Message + "\n");
                }
            } else {
                textArea.append("D497的值不为1，无需执行操作。\n");
            }
        } else {
            textArea.append("读取D497失败：" + readD497.Message + "\n");
        }

        melsec_net.ConnectClose();
    }

    private String byteArrayToHexString(byte[] bytes) {
        StringBuilder stringBuilder = new StringBuilder();
        for (byte b : bytes) {
            stringBuilder.append(String.format("%02X ", b));
        }
        return stringBuilder.toString();
    }


    private void writeToPLC() {
        MelsecA1ENet melsec_net = new MelsecA1ENet("192.168.1.11", 3000);
        melsec_net.SetPersistentConnection();

        // 在这里执行写入PLC的操作，可以根据需求添加相应的写入代码

        melsec_net.ConnectClose();
    }
}
